<?php
namespace Admin\Controller;
use Think\Controller;

class IndexController extends CommonController {

	//显示网站的统计数据
	public function index(){

		// 注册人数显示选择
		if (isset($_GET['type'])) {

			//查看近30填注册人数
			if ($_GET['type']=='days') {
				$title = '最近30天注册人数';
				$time =$this->days();
				$str = '';
				for ($i=29; $i >= 0; --$i) {
					$str .= $time[$i].',';
				}
				$str = rtrim($str, ',');
				$this->assign('month', $str);
				$this->assign('alive','days');
			}

			//查看近6年填注册人数
			if ($_GET['type']=='years') {
				$title = '最近6年注册人数';
				$time =$this->years();
				$str = '';
				for ($i=5; $i >= 0; --$i) {
					$str .= $time[$i].',';
				}
				$str = rtrim($str, ',');
				$this->assign('alive','years');
				$this->assign('month', $str);
			}
		}else{
			// 默认显示最近12个月注册人数
			$title = '最近12个月注册人数';
			$month = $this->getLast12MonNum();
			$str = '';
			for ($i=11; $i >= 0; --$i) {
				$str .= $month[$i].',';
			}
			$str = rtrim($str, ',');
			$this->assign('alive','month');
			$this->assign('month', $str);
		}
		$this->assign('title', $title);

		$user = M('user_info');

		// 查询总人数
		$count = $user->field('count(*) sex')->select();
		$count = $count[0]['sex'];

		// 女性
		$count0 = $user->where('sex = 0')->select();
		if ($count0) {
			$sex0 = round(count($count0)/$count*100);
		} else {
			$sex0 = 0;
		}

		// 男性
		$count1 = $user->where('sex = 1')->select();
		if ($count1) {
			$sex1 = round(count($count1)/$count*100);
		} else {
			$sex1 = 0;
		}

		// 保密
		$count2 = $user->where('sex = 2')->select();
		if ($count2) {
			$sex2 = round(count($count2)/$count*100);
		} else {
			$sex2 = 0;
		}

		// 查询各年龄区间的人数
		$allAge = $user->field('count(*) num')->select();
		$allAgeNum = $allAge[0]['num'];

		$age = round(($this->getAgeNum(0,18))/$allAgeNum*100);
		$this->assign('age0_18', $age);

		$age = round(($this->getAgeNum(18,30))/$allAgeNum*100);
		$this->assign('age18_30', $age);

		$age = round(($this->getAgeNum(30,40))/$allAgeNum*100);
		$this->assign('age30_40', $age);

		$age = round(($this->getAgeNum(40,50))/$allAgeNum*100);
		$this->assign('age40_50', $age);

		$age = round(($this->getAgeNum(50,60))/$allAgeNum*100);
		$this->assign('age50_60', $age);

		$age = round(($this->getAgeNum(60))/$allAgeNum*100);
		$this->assign('age60', $age);

		$this->assign('sex0', $sex0);
		$this->assign('sex1', $sex1);
		$this->assign('sex2', $sex2);

		// 得到省份人数
		$province = $this->getProvinces();
		$this->assign('province', $province);
		$this->assign('test', '数据分析');
		$this->display();
	}

	/**
	 * [getAgeNum 获得某区间年龄人数]
	 * @param  [int] $start [起始年龄]
	 * @param  [int] $end   [终止年龄]
	 * @return [int]        [返回查询到的人数]
	 */
	protected function getAgeNum($start, $end=null){
			//0为现在
			if ($start == 0) {
				$startT = strtotime('now');
			}else{
				$startT = strtotime('-'.$start.' years');
			}

			$user = M('user_info');

			if ($end == null) {
				// 没有传递终止年龄
				$num = $user->field('count(*) num')->where("$startT > birthday ")->select();
			} else {

				$endT = strtotime('-'.$end.' years');
				$where['birthday'] = array(array('elt',$startT),array('gt',$endT),'and');
				$num = $user->field('count(*) num')->where($where)->select();
			}

			return $num[0]['num'];
	}

	/**
	 * [getLast12MonNum 查询近12月的注册人数]
	 * @return [array] [返回12月各月人数]
	 */
	public function getLast12MonNum(){
		$user = M('user');
		// 循环获得各月月初的时间戳, 并查询得到人数
		for ($i=0; $i < 12; $i++) {
			$month = mktime(0,0,0,date('m')-$i,1,date('Y'));
			if ($i == 0) {
				$where['regtime'] = array('egt', $month);
				$res = $user->field('count(*) num')->where($where)->select();

			}else{
				$monthEnd = mktime(0,0,0,date('m')-$i+1,1,date('Y'));
				$where['regtime'] = array(array('egt', $month), array('lt', $monthEnd), 'and');
			}
			$res = $user->field('count(*) num')->where($where)->select();

			$time[] = $res[0]['num'];
		}

		return $time;
	}

	/**
	 * [days 查询近30天注册人数]
	 * @return [array] [返回30天, 每天人数]
	 */
	public function days(){
		$user = M('user');
		// 循环得到每天开始的时间戳, 并查询人数
		for ($i=0; $i < 30; $i++) {
			$month = mktime(0,0,0,date('m'),date('d')-$i,date('Y'));
			if ($i == 0) {
				$where['regtime'] = array('egt', $month);
				$res = $user->field('count(*) num')->where($where)->select();

			}else{
				$monthEnd = mktime(0,0,0,date('m'),date('d')-$i+1,date('Y'));
				$where['regtime'] = array(array('egt', $month), array('lt', $monthEnd), 'and');
			}
			$res = $user->field('count(*) num')->where($where)->select();

			$time[] = $res[0]['num'];
		}

		return $time;
	}

	/**
	 * [years 查询近6年注册人数]
	 * @return [array] [返回每年的人数]
	 */
	public function years(){
		$user = M('user');
		// 循环每年的年初时间戳, 并查询人数
		for ($i=0; $i < 6; $i++) {
			$month = mktime(0,0,0,1,1,date('Y')-$i);
			if ($i == 0) {
				$where['regtime'] = array('egt', $month);
				$res = $user->field('count(*) num')->where($where)->select();

			}else{
				$monthEnd = mktime(0,0,0,1,1,date('Y')-$i+1);
				$where['regtime'] = array(array('egt', $month), array('lt', $monthEnd), 'and');
			}
			$res = $user->field('count(*) num')->where($where)->select();

			$time[] = $res[0]['num'];
		}

		return $time;
	}

	/**
	 * [getProvinces 获得前12地区注册人数]
	 * @return [array] [返回前12省份的各省份人数]
	 */
	public function getProvinces(){
		$user = M('user_info');
		$total = $user->field('count(*) a')->select();
		$totalnum = $total[0]['a'];// 总人数

		// 查询最多地区人数
		$res = $user->group("SUBSTRING_INDEX(area,'-',1)")->field("count(SUBSTRING_INDEX(area,'-',1)) a ")->limit(1)->order("a desc")->select();// 最多人数

		// var_dump($res);
		// 提交显示百分百基点
		$pec = 0.9/$res[0]['a'] ;
		// 查询省份人数, 按省份分组, 统计人数
		$res = $user->group("SUBSTRING_INDEX(area,'-',1)")->field("SUBSTRING_INDEX(area,'-',1) area,concat(100*count(SUBSTRING_INDEX(area,'-',1))*".$pec.",'%') num, concat(100*count(SUBSTRING_INDEX(area,'-',1))/".$totalnum.",'%') na")->limit(12)->order("count(SUBSTRING_INDEX(area,'-',1)) desc")->select();

		// var_dump($res);
// die;
		return $res;

	}

}
